package org.ndx.majick.ui.enumeration;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;

import org.ndx.majick.properties.Property;
import org.ndx.majick.ui.IClassUIProvider;
import org.ndx.majick.ui.IPropertyUIMetadata;
import org.ndx.majick.ui.object.ObjectUIProvider;

public class EnumUIProvider extends ObjectUIProvider implements
		IClassUIProvider {
	@Override
	public EnumPropertyMetadata createMetadata() {
		return new EnumPropertyMetadata();
	}

	@Override
	public JComponent getEditor(IPropertyUIMetadata metadata, Property<?> source) {
		Property<? extends Enum> enumSource = (Property<? extends Enum>) source;
		EnumPropertyMetadata enumMeta = (EnumPropertyMetadata) metadata;
		JComboBox returned = new JComboBox(new EnumPropertyBackedComboBoxModel(enumSource, enumMeta));
		returned.setEditable(false);
		configureEditor(returned, metadata, enumSource);
		return returned;
	}

	public JComponent getViewer(final IPropertyUIMetadata metadata, final Property<?> source) {
		final JLabel returned = new JLabel();
		returned.setText(source.get().toString());
		source.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				returned.setText(source.get().toString());
			}
		});
		configureViewer(returned, metadata, source);
		return returned;
	}
	
	
}
